// 设置公共的全局变量
let uri = "http://localhost:8080/cx/";

let userToken = "";

let client_ip = "";

/**
 * 判断一个字符串是否为空
 * @param s
 * @returns {boolean}
 */
function isNull(s) {
    return s == null || s === '';

}

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            break;
        default:
            return 0;
            break;
    }
}

/**
 * 时间戳转时间字符串
 * @param timestamp 时间戳
 * @param dt 类型 date日期 datetime时间
 */
function dateFormat(timestamp, dt) {
    if (isNull(timestamp)) {
        return timestamp;
    }
    // 格式化
    let date = new Date(timestamp);
    var year = date.getFullYear();
    var mon = date.getMonth() + 1;
    var day = date.getDate();
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();

    if (dt === 'datetime') {
        return year + "-" + mon + "-" + day + " "
            + h + ":" + m + ":" + s;
    } else {
        return year + "-" + mon + "-" + day;
    }
}

function getIp() {
    $.ajax({
        url: 'http://ipinfo.io/ip',
        type: 'get',
        dataType: 'html',
        success: function (data) {
            client_ip = data;
            localStorage.setItem("client_ip", data);
        }
    })
}

$(function () {

})

function toLogin() {
    window.location.href = "user/user-login.html";
}

function logout() {
    alert("logout");
    userToken = "";
    localStorage.removeItem("userToken");
    toLogin();
}

function toastMsg(text, icon, time) {
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: false,
        hideAfter: time,
        stack: 3,
        position: 'top-center',
        textAlign: 'center',
        loader: false,
        bgColor: '#BDBDBD',
        // textColor:
        /*beforeShow:function (){
            alert('before');
        }*/

    });
}

function myToast(text) {
    // 判断toast div是否已经存在
    if (!$('#m-toast').length){
        let toast_div = "<div style='position:absolute;z-index:999;left:50%;bottom:45px;height:70px;width:200px;text-align: center;vertical-align: middle;'>";
        toast_div += "<div id='m-toast' style='position:relative;left:-50%;background-color: #4d4d4d;opacity: 0.75;font-size: 14px;color: #E0ECFF;" +
            "vertical-align: middle;line-height:60px;height: 60px;border-radius: 7px;padding:1px 5px;display: none;'>" + text + "</div></div>";
        $('body').append(toast_div);
        // $('#m-toast').fadeIn(2000).delay(2000).slideUp(1000).show(1000).fadeOut(1000);
        $('#m-toast').fadeIn(1000).delay(1500).slideUp(1000);
    }else{
        $('#m-toast').html(text);
        $('#m-toast').fadeIn(1000).delay(1500).slideUp(1000);
    }

}

function toast(toastIn, delay, out) {
    myToast();
}

function ajaxGet(url, token) {
    $.ajax({
        url: uri + url,
        type: "get",
        beforeSend: function (xhr) {
            // 通过xhr设置header  或者其他设置比如禁用按钮
            if (!isNull(token)) {
                xhr.setRequestHeader("token", token);
            }
        },
        dataType: "json",
        success: function (data, status) {
            console.log("返回数据：" + JSON.stringify(data) + "，状态：" + status);
        },
        error: function (xhr, status) {
            console.log("失败" + status)
        }
    })
}

function ajaxPost(url, token, paramJson) {
    $.ajax({
        url: uri + url,
        type: "POST",
        contentType: "application/json",
        beforeSend: function (xhr) {
            if (!isNull(token)) {
                xhr.setRequestHeader("token", token);
            }
        },
        data: paramJson,
        dataType: "json",
        // status 200 时返回
        success: function (data) {
            console.log("返回数据：" + JSON.stringify(data) + "，状态：" + status);
            if (data.code === "0") {
                // 调用方js拿不到该返回数据，因为是异步，这个返回的时候，调用方js早已经执行完了
                return data.data;
            }
        },
        error: function (xhr, status) {
            console.log("失败" + status)
        },
        // complete:function (xhr,status) 返回具体的status 200  404  401 500等
    })
}

